﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using Newtonsoft.Json;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for OutSourceLender
    /// </summary>
    public class OutSourceLender : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //user defined function
        #region private method

        public void OutSourceLenderSelect()
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);

                DataSet ds = OutSourceLenderHandler.GetOutSourceLenderList(pageIndex, pageSize);                 
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int OutSourceLenderAdd(string OutSourceLender)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = OutSourceLenderHandler.AddOutSourceLender(OutSourceLender);
            }
            catch (Exception)
            {
                throw;
            }
            return intReturnValue;
        }

        public int OutSourceLenderEdit(int LenderID, string OutSourceLender)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = OutSourceLenderHandler.EditOutSourceLender(LenderID, OutSourceLender);
            }
            catch (Exception)
            {
                throw;
            }
            return intReturnValue;
        }

        public bool OutSourceLenderDelete(int LenderID)
        {
            bool flag = false;
            try
            {
                flag = OutSourceLenderHandler.DeleteOutSourceLender(LenderID);
            }
            catch (Exception)
            {
                throw;
            }
            return flag;
        }

        #endregion
    }
}